package com.webcollector.webcollector.net;

import cn.wanghaomiao.seimi.annotation.Crawler;
import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.seimi.struct.Request;
import cn.wanghaomiao.seimi.struct.Response;

import com.alibaba.fastjson.JSONObject;
import com.webcollector.webcollector.bean.Card;
import com.webcollector.webcollector.bean.Top;
import java.util.List;

import com.webcollector.webcollector.service.TopServiceImpl;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.seimicrawler.xpath.JXDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

@Crawler(name = "basic")
@Slf4j
public class Basic extends BaseSeimiCrawler {

    @Autowired
    private TopServiceImpl topService;

    @Override
    public String[] startUrls() {
        return new String[]{"http://s.weibo.com/top/summary?cate=realtimehot"};
    }

    @Override
    public void start(Response response) {
        JXDocument doc = response.document();
        try {
            List<Object> titleList = doc.sel("//td[@class='td-02']/a/text()");
            List<Object> heatList = doc.sel("//td[@class='td-02']/span/text()");
            List<Object> urlList = doc.sel("//td[@class='td-02']/a/[@href]");
            bachInstet(titleList,heatList,urlList);

        } catch (Exception e) {
            log.warn("start ",e);
        }
    }
    @Scheduled(cron = "0/01 * * * * ?")
    public void callByCron(){
        push(Request.build(startUrls()[0],"start").setSkipDuplicateFilter(true));
    }

    private void bachInstet( List<Object> titleList,List<Object> heatList,List<Object> urlList){
        topService.bachInsert(titleList.subList(1,titleList.size()),heatList,urlList.subList(1,urlList.size()));
        log.info("bachInsert size={}",titleList.size());
    }
}
